const { CleanWebpackPlugin } = require('clean-webpack-plugin')
const path = require('path')
let webpack = require('webpack') // 引入webpack库
let BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
const LodashModuleReplacementPlugin = require('lodash-webpack-plugin')
const CompressionWebpackPlugin = require('compression-webpack-plugin')
const TerserPlugin = require('terser-webpack-plugin')

const resolve = dir => {
  return path.join(__dirname, dir)
}

// 项目部署基础
// 默认情况下，我们假设你的应用将被部署在域的根目录下,
// 例如：https://www.my-app.com/
// 默认：'/'
// 如果您的应用程序部署在子路径中，则需要在这指定子路径
// 例如：https://www.foobar.com/my-app/
// 需要将它改为'/my-app/'
// iview-admin线上演示打包路径： https://file.iviewui.com/admin-dist/
const PUBLIC_PATH = process.env.NODE_ENV === 'production'
  ? '/'
  : '/'

module.exports = {
  publicPath: PUBLIC_PATH,
  lintOnSave: true,
  chainWebpack: config => {
    config.resolve.alias
      .set('@', resolve('src')) // key,value自行定义，比如.set('@@', resolve('src/components'))
      .set('_c', resolve('src/components'))
    config.optimization.minimize(true)
    config.plugin('ignore')
      .use(new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/)) // 忽略/moment/locale下的所有文件
    if (process.env.NODE_ENV === 'production') {
      config.plugin('analyzer')
        .use(new BundleAnalyzerPlugin()) // 使用webpack-bundle-analyzer 生成报表
    }
    config.plugin('loadshReplace')
      .use(new LodashModuleReplacementPlugin()) // 优化lodash
  },
  css: {
    sourceMap: process.env.NODE_ENV !== 'production',
    extract: process.env.NODE_ENV !== 'development',
    loaderOptions: {
      less: {
        javascriptEnabled: true
      }
    }
  },
  // 设为false打包时不生成.map文件
  productionSourceMap: false,
  configureWebpack: config => {
    if (process.env.NODE_ENV === 'production') {
      const plugins = []
      plugins.push(new CleanWebpackPlugin())
      plugins.push(
        new CompressionWebpackPlugin({
          algorithm: 'gzip',
          test: /\.(js|css|json|txt|html|ico|svg)(\?.*)?$/i,
          threshold: 10240,
          minRatio: 0.8
        })
      )
      config.plugins = [...config.plugins, ...plugins]

      config.optimization = {
        ...config.optimization,
        minimize: true,
        minimizer: [new TerserPlugin()]
      }
    }
    config.optimization = {
      ...config.optimization,
      splitChunks: {
        cacheGroups: {
          common: {
            name: 'common',
            chunks: 'all',
            minSize: 0,
            minChunks: 2,
            maxInitialRequests: 5
          },
          vendor: {
            name: 'vender',
            test: /[\\/]node_modules[\\/]/, // 匹配node模块中匹配的的模块
            chunks: 'initial',
            priority: 10, // 设置匹配优先级，数字越大，优先级越高
            enforce: true
          }
        }
      }
    }
  },
  devServer: {
    disableHostCheck: true,
    clientLogLevel: 'warning',
    proxy: {
      '/api/': {
        target: 'http://103.21.143.136:81',
        // target: 'http://taotao98k.vip.qydev.com',
        ws: true, // proxy websockets
        changeOrigin: true, // needed for virtual hosted sites
        pathRewrite: {
          '^/api/': '/' // rewrite path
        }
      },
      '/uploaders': {
        target: 'http://mix.qjcode.com', // 千嘉
        ws: true, // proxy websockets
        changeOrigin: true, // needed for virtual hosted sites
        pathRewrite: {
          '^/uploaders/': '/' // rewrite path
        }
      },
      '/foo': {
        target: '<other_url>'
      }
    }
  }
}
